﻿using System;
using System.Windows.Input;
using Peter.Common;
using Peter.Common.Interfaces;

namespace Peter.Sdw.Logic
{
   /// <summary>
   /// Model used for adding an add button to the editor.
   /// </summary>
   public class EditorAddModel : ViewModelBase
   {
      private RelayCommand m_CmdAdd;

      /// <summary>
      /// Occurs when the user presses the add button.
      /// </summary>
      public event EventHandler AddRequested;

      /// <summary>
      /// Gets or Sets the peter editor.
      /// </summary>
      public IPeterEditor Editor { get; set; }

      /// <summary>
      /// Gets the add command.
      /// </summary>
      public ICommand AddCommand
      {
         get { return this.m_CmdAdd ?? (this.m_CmdAdd = new RelayCommand (this.OnAdd, this.CanAdd)); }
      }

      /// <summary>
      /// Checks to see if the add command can execute.
      /// </summary>
      /// <param name="obj">Command parameter.</param>
      /// <returns>True if the command can execute, otherwise false.</returns>
      protected virtual bool CanAdd (object obj)
      {
         if (this.Editor == null) return false;
         return !string.IsNullOrEmpty (this.Editor.Text);
      }

      /// <summary>
      /// Execution of the add command.
      /// </summary>
      /// <param name="obj">Command parameter.</param>
      protected virtual void OnAdd (object obj)
      {
         if (this.AddRequested != null)
            this.AddRequested (this, new EventArgs ());
      }
   }
}
